<?php

namespace App\Http\Controllers\Frontend\V10\UnionLogin;

use App\Library\String\UnionLoginTools;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Constant\SmsTemplate;
use App\Library\String\Strings;
class GatherController extends Controller
{

//    const LOAD_URLS = "https://manage.longloan.cn/#/fish";
    const LOAD_URLS = "https://manage.longloan.cn/#/download";
    /**
     * 撞库返回code
     */

    const HIT_CODE_SUCCESS_NEW = 1; //聚好贷新用户
    const HIT_CODE_FAILURE_PHONE = 9001;//聚好贷用户手机号码错误
    const HIT_CODE_FAILURE_HAVEPHONE = 9002;//聚好贷调用手机号已存在
    const HIT_CODE_FAILURE_SIGN = 9003;//签名错误
    const HIT_CODE_FAILURE_FROM = 9004;//渠道号错误
    const HIT_CODE_FAILURE_IP = 9005;//IP地址错误
    const HIT_CODE_FAILURE_OLD = 9007;//老用户


    /**
     * 连登返回code
     */
    const UINON_CODE_SUCCESS = 1; //联合登录成功（返回url）
    const UINON_CODE_FAIL_PHONE = 9001;//聚好贷用户手机号码错误
    const UINON_CODE_FAIL_HAVEPHONE = 9002;//聚好贷调用手机号已存在
    const UINON_CODE_FAIL_SIGN = 9003;//签名错误
    const UINON_CODE_FAIL_FROM = 9004;//渠道号错误
    const UINON_CODE_FAIL_IP = 9005;//IP地址错误

    /**
     * 连登返回其他错误
     */
    const GATHER_CODE_FAILURE_THINGS = 9006;//其他错误

    const GATHER_CODE_FAILURE_MESS = 9008;//参数错误



    public function index(Request $request){
//        $data = $request->data;
        $data = [
            "mid"=>"juxin",
            "phone"=>"15898558777",
            'ip' => '192.168.1.1'
        ];
        //加密
        $sign = UnionLoginTools::encryptGatherBank($data);
        $url = 'https://learnku.com/articles/3729/use-laravel-queue-to-understand-the-knowledge';
        $data = [
            'a' => '测试',

            'b'=>$url
        ];
        $url = json_encode($data,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
        dd($url);

    }
    /**
     * Description: 接收聚好贷用户验证
     * Author: ShengZengGuang
     * DateTime: 2019-03-15 11:43
     * @param Request $request
     * @return mixed
     */
    public function check(Request $request)
    {
        try{
            $data_save['mid'] = $request->input('mid','');
            $data_save['phone'] = $request->input('phone','');
            $data_save['ip'] = $request->input('ip','');
            $data_save_sign = $request->input('sign', '');
            $missParams = '';
            foreach($data_save as $key=>$value){
                if(!$request->filled($key)){
                    $missParams = $value;
                    break;
                }
            }
            if($missParams){
                $code = self::GATHER_CODE_FAILURE_MESS;
                $message = '请求参数'.$missParams.'为空';
                return json_encode(['resultCode'=>$code,'resultMsg'=>$message],JSON_UNESCAPED_UNICODE);
            }

            //验签
            $has_pass = UnionLoginTools::verifyGatherBank($data_save,$data_save_sign);
            if(!$has_pass){
                $code = self::HIT_CODE_FAILURE_SIGN;
                $message = '数据签名错误';
                return json_encode(['resultCode'=>$code,'resultMsg'=>$message],JSON_UNESCAPED_UNICODEs);
            }
            $raw_mobile = $data_save['phone'];
            $userModel = new User();
            $userModel->setConnection('backend');
            $has_register = $userModel->where(['phone_num'=>$raw_mobile])->value('id');
            if($has_register) {
                $code = self::HIT_CODE_FAILURE_OLD;
                $message = '聚好贷借款老用户';
                return json_encode(['resultCode'=>$code,'resultMsg'=>$message],JSON_UNESCAPED_UNICODE);
            }
            $code = self::HIT_CODE_SUCCESS_NEW;
            $message = '用户可接收';
            return json_encode(['resultCode'=>$code,'resultMsg'=>$message],JSON_UNESCAPED_UNICODE);
        }catch (\Exception $e){
            $code = self::GATHER_CODE_FAILURE_THINGS;
            return json_encode(['resultCode'=>$code,'resultMsg'=>$e->getMessage()],JSON_UNESCAPED_UNICODE);
        }
    }
    /**
     * Description: 接收聚好贷用户注册
     * Author: ShengZengGuang
     * DateTime: 2019-03-15 11:43
     * @param Request $request
     * @return mixed
     */
    public function user_register(Request $request)
    {
        $url = self::LOAD_URLS.'?channel='.User::SOURCE_JU_HAO_DAI;
        try{

            $data_save['mid'] = $request->input('mid','');
            $data_save['phone'] = $request->input('phone','');
            $data_save['ip'] = $request->input('ip','');
            $data_save_sign = $request->input('sign', '');
            $missParams = '';
            foreach($data_save as $key=>$value){
                if(!$request->filled($key)){
                    $missParams = $value;
                    break;
                }
            }
            if($missParams){
                $code = self::GATHER_CODE_FAILURE_MESS;
                $message = '请求参数'.$missParams.'为空';
                return json_encode(['resultCode'=>$code,'resultMsg'=>$message],JSON_UNESCAPED_UNICODE);
            }
            //验签
            $has_pass = UnionLoginTools::verifyGatherBank($data_save,$data_save_sign);
            if(!$has_pass){
                $code = self::UINON_CODE_FAIL_SIGN;
                $message = '数据签名错误';
                return json_encode(['resultCode'=>$code,'resultMsg'=>$message],JSON_UNESCAPED_UNICODE);
            }
            $mobile = $data_save['phone'];
//            $username = $json_data['username'];
            if(User::where('phone_num',$mobile)->value('id')){
                $code = self::UINON_CODE_FAIL_HAVEPHONE;
                $message = '已注册用户重复注册';
                return json_encode(['resultCode'=>$code,'resultMsg'=>$message,'ResultUrl'=>$url],JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
            }
            $rand_code =Strings::randomCode(6);
            User::createUser([
                'phone_num' => $mobile,
                'password' => $rand_code,
                'source' => User::SOURCE_JU_HAO_DAI,
            ]);
           // $mobile = '15737893304';
            //推送聚好贷用户，快闪卡贷自动注册成功
            SmsTemplate::send(SmsTemplate::TEMPLATE_RZG_REGISTER,$mobile,['name'=>$mobile,'password'=>$rand_code]);
            $code = self::UINON_CODE_SUCCESS;
            $message = '成功';
            return json_encode(['resultCode'=>$code,'resultMsg'=>$message,'ResultUrl'=>$url],JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
        }catch (\Exception $e){
            $code = self::GATHER_CODE_FAILURE_THINGS;
            return json_encode(['resultCode'=>$code,'resultMsg'=>$e->getMessage()],JSON_UNESCAPED_UNICODE);
        }
    }
}
